# FROM指令必须是第一条指令 表示父镜像 （ADD BY donhui）
FROM java:8u45-jdk

# RUN指令用来运行命令，每个Dockerfile可以有多个RUN指令
# 更新APT源，安装wget git curl zip，同时强制释放锁（如果出现意外情况）（ADD BY donhui）
RUN apt-get update && apt-get install -y wget git curl zip && rm -rf /var/lib/apt/lists/*

# ENV指令用来设置环境变量（ADD BY donhui）
ENV JENKINS_HOME /var/jenkins_home

# Jenkins is ran with user `jenkins`, uid = 1000
# If you bind mount a volume from host/vloume from a data container, 
# ensure you use same uid
# 添加用户jenkins（ADD BY donhui）
RUN useradd -d "$JENKINS_HOME" -u 1000 -m -s /bin/bash jenkins

# Jenkins home directoy is a volume, so configuration and build history 
# can be persisted and survive image upgrades
# 创建一个挂载点，持久化JENKINS_HOME（ADD BY donhui）
VOLUME /var/jenkins_home

# `/usr/share/jenkins/ref/` contains all reference configuration we want 
# to set on a fresh new installation. Use it to bundle additional plugins 
# or config file with your custom jenkins Docker image.
RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d


COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy

ENV JENKINS_VERSION 1.609.1
ENV JENKINS_SHA 698284ad950bd663c783e99bc8045ca1c9f92159

# could use ADD but this one does not check Last-Modified header 
# see https://github.com/docker/docker/issues/8331
# 下载jenkins.war并在下载完成后进行sha1sum校验（ADD BY donhui）
RUN curl -fL http://mirrors.jenkins-ci.org/war-stable/$JENKINS_VERSION/jenkins.war -o /usr/share/jenkins/jenkins.war \
  && echo "$JENKINS_SHA /usr/share/jenkins/jenkins.war" | sha1sum -c -

ENV JENKINS_UC https://updates.jenkins-ci.org
ENV JENKINS_UC_DOWNLOAD $JENKINS_UC/download
RUN chown -R jenkins "$JENKINS_HOME" /usr/share/jenkins/ref

# EXPOSE命令用来暴露端口，jenkins master使用8080端口，jenkins slave连接master使用50000端口（ADD BY donhui）
# for main web interface:
EXPOSE 8080

# will be used by attached slave agents:
EXPOSE 50000

ENV COPY_REFERENCE_FILE_LOG /var/log/copy_reference_file.log
RUN touch $COPY_REFERENCE_FILE_LOG && chown jenkins.jenkins $COPY_REFERENCE_FILE_LOG

# USER指令，指定运行容器时的用户名或UID，后续的 RUN 也会使用指定用户。（ADD BY donhui）
USER jenkins

COPY jenkins.sh /usr/local/bin/jenkins.sh
# ENTRYPOINT 容器启动后执行jenkins.sh脚本（ADD BY donhui）
ENTRYPOINT ["/usr/local/bin/jenkins.sh"]

# from a derived Dockerfile, can use `RUN plugin.sh active.txt` to setup /usr/share/jenkins/ref/plugins from a support bundle
COPY plugins.sh /usr/local/bin/plugins.sh
